﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Space
{
    public class FlagShip : NewtonShip, IBuildable
    {
        const float BombRange = 10;

        public override bool Visible
        {
            get
            {
                return Built;
            }
        }

        public override bool Alive
        {
            set
            {
                if (value == false) Built = false;
                base.Alive = true;
            }
        }

        enum Guns
        {
            Main, Bomb, Laser
        }

        Gun[] guns = new Gun[3];
        public Gun MainGun { get { return guns[(int)Guns.Main]; } set { guns[(int)Guns.Main] = value; } }
        public Gun BombGun { get { return guns[(int)Guns.Bomb]; } set { guns[(int)Guns.Bomb] = value; } }
        public Gun LaserGun { get { return guns[(int)Guns.Laser]; } set { guns[(int)Guns.Laser] = value; } }

        public int Level { get; set; }
        int experience;
        public int Experience
        {
            get
            {
                return experience;
            }
            set
            {
                experience = value;

                int etl = ExperienceToLevel(Level);
                if (experience < 0)
                {
                    Level--;
                    experience += ExperienceToLevel(Level);
                }
                else if (experience >= etl)
                {
                    experience -= etl;
                    Level++;
                }
            }
        }

        public int ExperienceToLevel(int level)
        {
            int baseExp = 10;
            for (int i = 0; i < level; i++)
                baseExp = (int)(baseExp * 1.4);
            return baseExp;
        }

        public bool Built { get; set; }
        public float BuildingTime { get; set; }

        public FlagShip(Game game)
            : base(game)
        {
            Alive = true;
            Built = true;
        }

        public static FlagShip Create(Game game, int level)
        {
            Game Game = game;

            FlagShip ship;

            switch (level)
            {
                case 10:
                    ship = new FlagShip(game)
                    {
                        HP = 10,
                        Speed = 0.1f,
                        FireInterval = 0.5f,

                        Model = Game.Content.Load<Model>("models/flagship"),
                        Acceleration = 0.001f,
                        TurnSpeed = 0.3f,

                    };
                    ship.MainGun = new MissileGun(ship)
                    {
                        FireInterval = 0.2f,
                        Range = 20,
                        Missile = new Missile(ship)
                        {
                            Damage = 1,
                            Ship = ship,
                            Speed = 0.15f,
                        }
                    };
                    ship.BombGun = new BombGun(ship)
                    {
                        FireInterval = 0.3f,
                        Range = BombRange,
                    };
                    ship.LaserGun = new LaserGun(ship)
                    {
                        FireInterval = 3.5f,
                        Range = 10,
                        Laser = new Laser(ship)
                        {
                            Damage = 1,
                            Ship = ship,
                            MaxThickness = 0.1f,
                            LifeTime = 3
                        }
                    };
                    break;
                default:
                case 1:
                    ship = new FlagShip(game)
                    {
                        HP = 10,
                        Speed = 10.0f,
                        FireInterval = 0.5f,

                        Model = Game.Content.Load<Model>("models/flagship"),
                        Acceleration = 14f,
                        TurnSpeed = 3000.0f,

                        BuildingTime = 5,
                    };
                    ship.MainGun = new MissileGun(ship)
                    {
                        FireInterval = 0.2f,
                        Range = 20,
                        Missile = new Missile(ship)
                        {
                            Damage = 1,
                            Ship = ship,
                            Speed = 0.15f,
                        }
                    };
                    ship.BombGun = new BombGun(ship)
                    {
                        FireInterval = 0.3f,
                        Range = BombRange,
                    };
                    ship.LaserGun = new LaserGun(ship)
                    {
                        FireInterval = 3.5f,
                        Range = 10,
                        Laser = new Laser(ship)
                        {
                            Damage = 1,
                            Ship = ship,
                            MaxThickness = 0.1f,
                            LifeTime = 3
                        }
                    };
                    break;
            }
            return ship;
        }

        public override void Update(float time)
        {
            if (Alive && Built)
            {
                foreach (Gun gun in guns)
                {
                    if (gun != null)
                    {
                        gun.Position = Position;
                        gun.Update(time);
                    }
                }
                base.Update(time);
            }
        }

        public void FirePlanetBomb()
        {
            Planet targetPlanet = null;

            Bomb b = new Bomb(this)
            {
                Position = Position,
                Damage = 1,
                Speed = 0.1f,
                SplashRadius = 2f,
                Ship = this
            };

            IData data = game.Services.GetService<IData>();

            foreach (SolarSystem s in data.SolarSystems)
            {
                foreach (Planet p in s.Planets.Where(p=>p.Player !=Player))
                {
                    if ((p.Position - Position).Length() <= BombRange)
                    {
                        targetPlanet = p;
                        goto outer;
                    }
                }
            }
        outer:
            if (targetPlanet != null)
            {
                b.TargetPosition = targetPlanet.Position;
            }
            else
            {
                b.TargetPosition = Position + 10 * new Vector3((float)Math.Cos(Angle), 0, (float)Math.Sin(Angle));
            }
            data.AddWeapon(b);
        }

        public void CapturePlanet()
        {

        }


    }
}
